package com.erp.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.ForeignKey;

import com.erp.dto.Department;
import com.erp.dto.Role;
import com.erp.dto.User;

@SuppressWarnings("serial")
@Entity
@Table(name = "t_user")
public class TUser implements java.io.Serializable {

	public TUser() {
	}

	public TUser(String name, String password) {
		this.name = name;
		this.password = password;
	}

	public TUser(User user) {
		this.id = user.getId();
		this.name = user.getName();
		this.password = user.getPassword();
		this.sex = user.getSex();

		Department department = user.getDepartment();
		if (department != null) {
			this.tDepartment = new TDepartment(user.getDepartment());
		}
		if (user.getRoles() != null) {
			List<Role> roles = user.getRoles();
			for (Role role : roles) {
				this.tRoles.add(new TRole(role));
			}
		}

		this.loginTime = user.getLoginTime();
		this.loginLocation = user.getLoginLocation();
		this.ip = user.getIp();
		this.phone = user.getPhone();
		this.email = user.getEmail();
		this.description = user.getDescription();
	}

	private Integer id;
	private String name;
	private String password;
	private Boolean sex;
	private TDepartment tDepartment;
	private List<TRole> tRoles = new ArrayList<TRole>(0);
	private Date loginTime;
	private String loginLocation;
	private String ip;
	private String phone;
	private String email;
	private String description;

	@Id
	@GeneratedValue
	@Column(name = "id")
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "name")
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "password")
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "sex")
	public Boolean getSex() {
		return sex;
	}

	public void setSex(Boolean sex) {
		this.sex = sex;
	}

	/**
	 * TDepartment属性，本类与TDepartment的多对一单向关联
	 * 
	 * @return
	 */
	@ManyToOne
	@JoinColumn(name = "department_id")
	@ForeignKey(name = "fk_parent")
	public TDepartment gettDepartment() {
		return tDepartment;
	}

	public void settDepartment(TDepartment tDepartment) {
		this.tDepartment = tDepartment;
	}

	/**
	 * TRoles属性，本类与TRole的多对多双向关系
	 * 
	 * @return
	 */
	@ManyToMany
	@JoinTable(name = "t_user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
	@OrderBy("id")
	public List<TRole> gettRoles() {
		return tRoles;
	}

	public void settRoles(List<TRole> tRoles) {
		this.tRoles = tRoles;
	}

	@Column(name = "loginTime")
	public Date getLoginTime() {
		return loginTime;
	}

	public void setLoginTime(Date loginTime) {
		this.loginTime = loginTime;
	}

	@Column(name = "ip")
	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	@Column(name = "loginLocation")
	public String getLoginLocation() {
		return loginLocation;
	}

	public void setLoginLocation(String loginLocation) {
		this.loginLocation = loginLocation;
	}

	@Column(name = "phone")
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Column(name = "email")
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(name = "description")
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}